package buyer

import (
	"net/url"
	"reflect"
	"runtime"
	"strconv"
	"strings"
)

type Tester struct {
	File     string            "文件名"
	Name     string            "测试名"
	Url      string            "提交地址"
	Request  map[string]string "请求数据"
	Response map[string]string "应答数据"
	//	Send     string            "发送数据"
	Receive map[string]string "接受数据"
	Result  string            "结果"
}

type NetInter interface {
	PostForm(url string, val url.Values)
}

type StreamInter interface {
	Reader(string) (string, []string)
	ReadData(string) string
	Writer(string, string) (int, error)
}

type JsonInter interface {
	FilterJsonFile(string) bool
	JsonDecoder(string)
}

func (t *Tester) ToURLValues() map[string][]string {
	vals := make(url.Values)
	for k, v := range t.Request {
		vals[k] = strings.Fields(v)
	}
	return vals
}

func (t *Tester) ToString() string {
	Where()
	var result string
	elem := reflect.ValueOf(t).Elem()
	for i := 0; i < elem.NumField(); i++ {

		result += strconv.Itoa(i)
		result += "|"
		result += elem.Type().Field(i).Name
		result += ":"
		result += elem.Field(i).String() + "\n"

		ran := elem.Field(i).Interface()
		switch ran.(type) {
		case map[string]string:
			{
				mapvalue := ran.(map[string]string)

				for k, v := range mapvalue {
					result += "  MapKey:"
					result += k
					result += "|MapValue:"
					result += v
					result += "\n"
				}
			}
		default:

		}
	}

	println(result)
	return result
}

func Where() {
	_, s, l, _ := runtime.Caller(1)
	print("call from: " + s + " line: ")
	println(l)
}

func Check(err error) {
	if err != nil {
		panic(err)
	}
}

func CheckResult(m1 map[string]string, m2 map[string]string) (result string) {
	result = "OK"
	if len(m1) != len(m2) {
		return "NG"
	}

	for k, v := range m1 {
		if strings.Compare(v, m2[k]) != 0 {
			result = "NG"
			break
		}
	}

	return result
}
